package com.lzy.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lzy.crm.base.date.DateUtil;
import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.dao.BankCardMapper;
import com.lzy.crm.dao.TradeFlowMapper;
import com.lzy.crm.dao.UserMapper;
import com.lzy.crm.dao.criteria.TradeFlowCriteria;
import com.lzy.crm.dao.criteria.UserCriteria;
import com.lzy.crm.domain.BankCard;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.domain.TradeFlow;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.TradeFlowService;
import com.lzy.crm.vo.TradeFlowVo;


@Service("tradeFlowService")
public class TradeFlowServiceImpl extends BaseServiceImpl<TradeFlow,TradeFlowCriteria> implements TradeFlowService {

	@Resource
	TradeFlowMapper tradeFlowMapper;
	@Resource
	UserMapper userMapper;
	@Resource
	BankCardMapper bankCardMapper;
	
	@Override
	public BaseMapper<TradeFlow, TradeFlowCriteria> getBaseMapper() {
		return tradeFlowMapper;
	}

	@Override
	public List<TradeFlowVo> getTradeFlowList(String gameUserId,String status,PageQuery pageQuery ) {
		List<TradeFlowVo> tradeFlowVoList = new ArrayList<TradeFlowVo>();
		List<TradeFlow> tradeFlowList = tradeFlowMapper.getTradeFlowList(gameUserId, status, pageQuery );
		/*for (int i = 0; i < tradeFlowList.size(); i++) {
			TradeFlow tradeFlow= tradeFlowList.get(i);
			TradeFlowVo tradeFlowVo= JsonUtil.fromJson(JsonUtil.toJson(tradeFlow), TradeFlowVo.class);
			PpUser user =userMapper.getById(tradeFlow.getUserId());
			tradeFlowVo.setPhone(user.getPhone());
			tradeFlowVo.setEmail(user.getEmail());
			tradeFlowVo.setUserName(user.getUserName());
			tradeFlowVoList.add(tradeFlowVo);
		}*/
		return tradeFlowVoList;
	}
	
	/**
	 * 
	 * @param user 登录管理员
	 * @param userId  代理商ID
	 * @param amount  打款金额
	 * @param bankCardId   打款银行卡
	 * @param remark  备注
	 * @return
	 */
	@Override
	@Transactional
	public int remitTrade(User user, Integer userId,double amount, Integer bankCardId ,String remark)throws Exception{
		
		BankCard bankCard= bankCardMapper.getById(bankCardId);
		User playerUser = userMapper.getById(userId);
		// 扣取打款的金额
		double newAmount = playerUser.getTiceng()-amount ;
		if(newAmount<0){
			// 超过体现的金额
			return 1;
		}
		playerUser.setTiceng(newAmount);
		userMapper.updateByPrimaryKeySelective(playerUser);
		// 生成打款记录信息
		TradeFlow tradeFlow =new TradeFlow();
		/*tradeFlow.setUserId(userId);
		tradeFlow.setGameUserId(playerUser.getGameUserId());*/
		tradeFlow.setAmount(amount);
		tradeFlow.setBankName(bankCard.getBankName());
		tradeFlow.setBankUserName(bankCard.getBankUserName());
		tradeFlow.setBankUserNo(bankCard.getBankUserNo());
		tradeFlow.setStatus("1");
		tradeFlow.setCreateTime(new Date());
		tradeFlow.setCreateUserId(user.getId());
		tradeFlow.setRemark(remark);
		tradeFlowMapper.insertSelective(tradeFlow);
		return 0;
	}
	
	
	/**
	 * 查询用户红包总金额
	 * @param userId
	 * @param gameId
	 * @return
	 */
	public Double getTradeFlowTotal(Integer userId, String gameId){
		
		return tradeFlowMapper.getTradeFlowTotal(userId, gameId);
		
	}
	
	
	/**
	 * 查看今天是否以提现
	 * @return
	 */
	public List<TradeFlow> getTradeFlow(Integer uid,String startDate,String endDate){
		Example<TradeFlowCriteria> example = new Example<TradeFlowCriteria>(TradeFlowCriteria.class);
		TradeFlowCriteria TradeFlowCriteria = example.createCriteria();
		TradeFlowCriteria.andUidEqualTo(uid+"");
		TradeFlowCriteria.andCreateTimeGreaterThan(startDate);  // 
		TradeFlowCriteria.andCreateTimeLessThan(endDate); // 小于
		return tradeFlowMapper.getListByExample(example);
	}
	
	
	/**
	 * 查看处理中的订单
	 * @return
	 */
	public List<TradeFlow> getTradeFlowByStatus(String status ,String startDate,String endDate){
		Example<TradeFlowCriteria> example = new Example<TradeFlowCriteria>(TradeFlowCriteria.class);
		TradeFlowCriteria TradeFlowCriteria = example.createCriteria();
		TradeFlowCriteria.andStatusEqualTo(status);
		TradeFlowCriteria.andCreateTimeGreaterThan(startDate);  // 
		TradeFlowCriteria.andCreateTimeLessThan(endDate); // 小于
		return tradeFlowMapper.getListByExample(example);
	}
	

}
